/**
 * 编辑车辆
 */
layui.use(['form', 'admin', 'HttpRequest','util'], function () {
    var form = layui.form;
    var admin = layui.admin;
    var HttpRequest = layui.HttpRequest;
    var $ = layui.$;
    var util = layui.util;


    // 获取设备详情
    var request = new HttpRequest(Feng.ctxPath + "/device/detail?id=" + Feng.getUrlParam("id"), 'get');
    var result = request.start();
    result.data.createTime = timetrans(result.data.createTime);
    result.data.deviceLastTime = timetrans(result.data.deviceLastTime);
    form.val('deviceForm', result.data);

    // 设备品牌下拉框回显
    layui.use(['form', 'upload', 'layer'],function(){
        var form = layui.form;
        $.ajax({
            url: Feng.ctxPath + '/device/brandList',
            dataType: 'json',
            type: 'get',
            success: function (data) {
                //使用循环遍历，给下拉列表赋值
                $.each(data.data, function (index, value) {
                    var option = new Option(value.name,value.devcieBrandId);
                    $('#deviceBrand').append(option);// 下拉菜单里添加元素
                });
                // 遍历select
                var deviceBrandId = result.data.deviceBrandId
                $("#deviceBrand").each(function() {
                    // this代表的是<option></option>，对option再进行遍历
                    $(this).children("option").each(function() {
                        // 判断需要对那个选项进行回显
                        if (this.value == deviceBrandId) {
                            // 进行回显
                            $(this).attr("selected","selected");
                        }
                    });
                })
                layui.form.render("select");//重新渲染 固定写法
            }
        })
    });

    // 设备类型下拉框回显
    layui.use(['form', 'upload', 'layer'],function(){
        var form = layui.form;
        $.ajax({
            url: Feng.ctxPath + '/dict/getDictListPage',
            data:{page:"1",limit:"10",dictTypeCode:"deviceType"},
            dataType: 'json',
            type: 'get',
            success: function (data) {
                //使用循环遍历，给下拉列表赋值
                $.each(data.data.rows, function (index, value) {
                    var option = new Option(value.dictName,value.dictCode);
                    $('#deviceType').append(option);// 下拉菜单里添加元素
                });
                // 遍历select
                var deviceType = result.data.deviceType
                $("#deviceType").each(function() {
                    // this代表的是<option></option>，对option再进行遍历
                    $(this).children("option").each(function() {
                        // 判断需要对那个选项进行回显
                        if (this.value == deviceType) {
                            // 进行回显
                            $(this).attr("selected","selected");
                        }
                    });
                })
                layui.form.render("select");//重新渲染 固定写法
            }
        })
    });

    function timetrans (res) {
        // 时间戳为0的情况下util.toDateString转换失败
        var date = new Date(res*1000);//如果date为13位不需要乘1000
        var Y = date.getFullYear() + '-';
        var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
        var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
        var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
        var m = (date.getMinutes() <10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
        var s = (date.getSeconds() <10 ? '0' + date.getSeconds() : date.getSeconds());
        return Y+M+D+h+m+s;
    };


    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {

        var request = new HttpRequest(Feng.ctxPath + "/device/edit", 'post', function (data) {

            Feng.success("修改成功!");
            admin.putTempData('formOk', true);
            admin.closeThisDialog();
            parent.location.reload();
        }, function (data) {
            Feng.error("修改失败!" + data.message);
            admin.closeThisDialog();
        });
        request.set(data.field);
        request.start(true);
    });

});